<template>
  <div class="container">
    <el-form
      :inline="true"
      :model="areaform"
      ref="taskform"
      class="demo-form-inline"
    >
      <el-form-item label="区域搜索">
        <el-input v-model="areaform.name" clearable placeholder="请输入" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button
          @click="newAndChange('新建')"
          size="mini"
          icon="el-icon-circle-plus-outline"
          style="background-color: #ff8437; color: #fff"
          >新建</el-button
        >
      </div>
      <el-table :data="areaList">
        <!-- 放置列 -->
        <el-table-column
          prop="index"
          align="center"
          width="50px"
          label="序号"
        />
        <el-table-column prop="name" align="center" label="区域名称" />
        <el-table-column
          prop="nodeCount"
          align="center"
          width="50px"
          label="点位数"
        />
        <el-table-column prop="remark" align="center" label="备注说明" />
        <el-table-column align="center" label="操作">
          <!-- 放置操作按钮 -->
          <template v-slot="{ row }">
            <el-button size="mini" type="text" @click="details(row)"
              >查看详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="newAndChange('修改', row.id)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              @click="del(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row
        type="flex"
        style="height: 60px"
        align="middle"
        justify="space-between"
      >
        <el-col :span="12"
          ><span style="font-size: 13px; color: #999; padding-left: 12px"
            >共{{ totalcount }}条记录 第{{ pageParams.pageIndex }}/{{
              totalpage
            }}页</span
          ></el-col
        >
        <el-col :push="12">
          <el-button-group>
            <el-button
              type="primary"
              @click="innPage"
              size="mini"
              :disabled="pageParams.pageIndex == 1"
              icon="el-icon-arrow-left"
              style="
                background-color: #d5ddf8;
                color: #000;
                margin-right: 12px;
                border: none;
              "
              >上一页</el-button
            >
            <el-button
              type="primary"
              @click="addPage"
              size="mini"
              :disabled="pageParams.pageIndex == this.totalpage"
              style="background-color: #d5ddf8; color: #000; border: none"
              >下一页<i class="el-icon-arrow-right el-icon--right"></i
            ></el-button>
          </el-button-group>
        </el-col>
      </el-row>
    </div>
    <!-- 新增区域的dialog -->
    <el-dialog
      title="新增区域"
      :visible.sync="dialogFormVisible"
      @close="closefn"
    >
      <el-form
        :model="addAreaForm"
        :rules="rules"
        label-width="80px"
        ref="areaform"
      >
        <el-form-item label="区域名称" prop="name">
          <el-input
            v-model="addAreaForm.name"
            maxlength="15"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input
            v-model="addAreaForm.remark"
            type="textarea"
            maxlength="40"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quitfn">取 消</el-button>
        <el-button type="primary" @click="submitfn">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 区域详情的dialog -->
    <el-dialog
      title="区域详情"
      :visible.sync="showAreadetails"
      @close="closefn1"
    >
      <div>
        <div class="single"><span class="title">区域名称:</span><span class="right" style="margin-left:30px">{{streetName}}</span></div>
        <div class="single"><span class="title">包含点位:</span><span class="right">
          <el-table :data="nodelist">
            <el-table-column prop="index" align="center" width="50px" label="序号"></el-table-column>
            <el-table-column prop="name" align="center" label="点位名称"></el-table-column>
            <el-table-column prop="vmCount" align="center" width="50px" label="设备数量"></el-table-column>
          </el-table>
          </span></div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAreaList,
  addArea,
  getAreaDetails,
  lookDetails,
  changeAreaDetails,
} from "@/api/node";
import { set } from 'vue';
export default {
  name: "Node",
  components: {},
  data() {
    return {
      streetName:'',
      showAreadetails:false,
      dialogFormVisible: false,
      totalcount: "",
      totalpage: "",
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
      },
      nodelist:[],
      addAreaForm: {
        name: "",
        remark: "",
      },
      areaform: {
        name: "",
      },
      areaList: [],
      rules: {
        name: [{ required: true, message: "请输入", trigger: "submit" }],
        remark: [{ required: true, message: "请输入", trigger: "submit" }],
      },
    };
  },
  async created() {
    this.getAreaList();
  },
  methods: {
    async getAreaList() {
      const res = await getAreaList(
        this.pageParams.pageIndex,
        this.pageParams.pageSize,
        this.areaform.name
      );
      this.areaList = res.currentPageRecords;
      this.totalpage = res.totalPage;
      this.totalcount = res.totalCount;
      this.areaList.forEach((item, index) => {
        this.$set(
          item,
          "index",
          this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + index + 1
        );
      });
      console.log(this.areaList);
    },
    addPage() {
      this.pageParams.pageIndex++;
      this.getAreaList();
    },
    innPage() {
      this.pageParams.pageIndex--;
      this.getAreaList();
    },
    search() {
      this.getAreaList();
    },
    closefn() {
      this.$refs.areaform.resetFields();
      this.dialogFormVisible = false;
    },
    closefn1(){

    },
    quitfn() {
      this.closefn();
    },
    submitfn() {
      this.$refs.areaform.validate(async (right) => {
        if (right) {
          if (!this.addAreaForm.id) {
            await addArea({
              regionName: this.addAreaForm.name,
              remark: this.addAreaForm.remark,
            });
            this.$message.success("添加成功");
            this.pageParams.pageIndex = 1;
            this.getAreaList();
            this.closefn();
          } else {
            const res = await changeAreaDetails(
              {
                regionName: this.addAreaForm.name,
                remark: this.addAreaForm.remark,
              },
              this.addAreaForm.id
            );
            if (res == true) {
              this.$message.success("修改成功");
              (this.addAreaForm = {
                name: "",
                remark: "",
              }),
                this.closefn();
              this.getAreaList();
            }
          }
        } else {
          this.$message.error(new Error());
        }
      });
    },
   async details(row) {
    this.streetName= row.name
    const { currentPageRecords }= await lookDetails(row.id);
    this.nodelist = currentPageRecords
    this.nodelist.forEach((item, index) => {
        this.$set(
          item,
          "index",
          this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + index + 1
        );
      });
    this.showAreadetails=true
    },
    async newAndChange(model, id) {
      if (model == "新建") {
        this.dialogFormVisible = true;
      } else {
        this.dialogFormVisible = true;
        this.addAreaForm = await getAreaDetails(id);
      }
    },
    del(id) {
      console.log(id);
    },
  },
  mounted(){
  //  setTimeout(()=>{
  //    this.details(id)
  //  },300)
  }
};
</script>

<style lang="scss" scoped>
.role-operate {
  padding: 10px;
}
::v-deep .el-dialog {
  padding: 20px;
  width: 50%;
  .dialog-footer {
    margin-top: -30px;
  }
  .single{
    display: flex;justify-content:flex-start;align-items: flex-start;
    margin-bottom: 20px;
    flex: 1;
    .right{
      width: 80%;
      margin-left: 10px;
      .el-table{
        margin-top: -15px;
      }
    }
  }
}
</style>
